﻿using System.ComponentModel.Composition;
using System.ComponentModel.DataAnnotations;

namespace OperationPlugins.Validators.DataAnnotations
{
    [Export(typeof(IUsePluginValidatorsAttributeAsDataAnnotationsValidationResult))]
    [DefaultPriority]
    internal sealed class DefaultIUsePluginValidatorsAttributeAsDataAnnotationsValidationResult : IUsePluginValidatorsAttributeAsDataAnnotationsValidationResult
    {
        /// <summary>
        /// Converts a <see cref="MessageContainer"/> object to <see cref="System.ComponentModel.DataAnnotations.ValidationResult"/> object.
        /// </summary>
        /// <param name="result">The <see cref="MessageContainer"/> object.</param>
        /// <returns>The <see cref="System.ComponentModel.DataAnnotations.ValidationResult"/> object.</returns>
        ValidationResult IUsePluginValidatorsAttributeAsDataAnnotationsValidationResult.AsDataAnnotationsValidationResult(MessageContainer result)
        {
            return result.ContainsErrors ?
                new ValidationResult(Message.ToString(result.Errors)) :
                ValidationResult.Success;
        }
    }
}
